package org.dream.service;

// Generated 2009-9-13 14:47:42 by Hibernate Tools 3.2.4.GA with hssgen

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dream.domain.Daydeal;
import org.dream.domain.Dealrecord;
import org.dream.domain.Dealrecord.Dealkind;
import org.dream.domain.Industry;
import org.dream.domain.Stock;
import org.dream.domain.ZtAnalysis;
import org.dream.persistence.DayDealDAO;
import org.dream.persistence.DaydealPersistenceImpl;
import org.dream.persistence.PersistenceService;
import org.dream.persistence.PersistenceServiceImpl;
import org.dream.util.DoubleUtil;

/**
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class StockServiceImpl implements StockService {
	PersistenceService persistenceService = new PersistenceServiceImpl();
	ImportService fileStockService = new ImportServiceImpl();
	private DayDealDAO dayDealDAO = new DaydealPersistenceImpl();

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.dream.market.service.StockService#getActivationListByKind()
	 */
	public List<Stock> findStocks(int kind) {
		if (kind == StockService.KIND_MYSELECT) {
			return persistenceService.findMySelectStocks();

		} else {
			return persistenceService.findAll(Stock.class);

		}

	}

	public Map<String, List<Industry>> findIndustryMap(Set<String> keys, boolean calculate) {
		Map<String, List<Industry>> map = new LinkedHashMap<String, List<Industry>>();
		List<Industry> list = findIndustrys(calculate);
		for (Industry industry : list) {
			if (null != keys && !keys.contains(industry.getBkmc())) {
				continue;
			}
			if (null == map.get(industry.getBkmc())) {
				map.put(industry.getBkmc(), new ArrayList<Industry>());
			}
			map.get(industry.getBkmc()).add(industry);
		}
		return map;
	}

	public Map<String, List<Industry>> CalIndustryMapValue(Map<String, List<Industry>> industryMap) {

		Map<String, List<Industry>> sortValueMap = new LinkedHashMap<String, List<Industry>>();
		// 最后一天数据，为了排序
		List<Industry> lastIndustries = new ArrayList<Industry>();

		for (String key : industryMap.keySet()) {

			List<Industry> industryList = industryMap.get(key);
			// 取出第一天上证
			Double value = persistenceService.getDaydealPoint(industryList.get(0).getDay(), "999999");
			if (null == value) {
				value = 100d;
			}
			for (int i = 0; i < industryList.size(); i++) {
				Industry industry = industryList.get(i);
				value = value * (1 + industry.getJzf() / 100);
				industry.setValue(value);
				if (i == industryList.size() - 1) {
					lastIndustries.add(industry);
				}
			}
		}
		Collections.sort(lastIndustries);
		for (Industry industry : lastIndustries) {
			sortValueMap.put(industry.getBkmc(), industryMap.get(industry.getBkmc()));
		}
		return sortValueMap;
	}

	public List<String> findIndustryBkmc(boolean calculate) {

		return persistenceService.findIndustryBkmc(calculate);
	}

	public List<Industry> findIndustrys(boolean calculate) {
		if (calculate) {
			return persistenceService.findAllIndustryByCalculate();
		} else {
			return persistenceService.findAllIndustry();
		}

	}

	public Stock getStockByCode(String code) {
		return persistenceService.getStockByCode(code);
	}

	public Map<String, Double> findCompByCode(String code, String startdate, String enddate, String standardCode,
			String standardData) {

		return null;
	}

	public List<Dealrecord> findAllDealRecord() {
		return persistenceService.findAllDealRecord();
	}

	public List<Dealrecord> findDealRecords(String stockCode, Dealkind dealkind) {
		List<Dealrecord> dealrecords = new ArrayList<Dealrecord>();
		for (Dealrecord dealrecord : findAllDealRecord()) {
			if (dealrecord.getBusinessName().equals("证券买入清算") || dealrecord.getBusinessName().equals("证券卖出清算")) {
				if (dealrecord.getStockCode().equals(stockCode)) {
					dealrecords.add(dealrecord);
				}

			}
		}
		return dealrecords;

	}

	public List<ZtAnalysis> findZtAnalysis(String day) {
		List<ZtAnalysis> analysises = new ArrayList<ZtAnalysis>();
		List<Daydeal> daydeals = dayDealDAO.findOverZf(day, 9.0);
		for (Daydeal daydeal : daydeals) {
			ZtAnalysis analysis = new ZtAnalysis();
			analysis.setStockName(daydeal.getName());
			analysis.setStockCode(daydeal.getCode());
			List<Daydeal> daydealAfters = dayDealDAO.findAfterDaydeal(day, daydeal.getCode());
			if (daydealAfters != null && daydealAfters.size() >= 1) {
				analysis.setRs2(daydealAfters.get(0).getRiseWidth());
				analysis.setRs2(DoubleUtil.changeDecimal(analysis.getRs2(), 1));
			}
			if (daydealAfters != null && daydealAfters.size() >= 2) {
				analysis.setRs3(daydealAfters.get(1).getRiseWidth());
				analysis.setRs3(DoubleUtil.changeDecimal(analysis.getRs3(), 1));
			}
			if (daydealAfters != null && daydealAfters.size() >= 3) {
				analysis.setRs4(daydealAfters.get(2).getRiseWidth());
				analysis.setRs4(DoubleUtil.changeDecimal(analysis.getRs4(), 1));
			}
			if (daydealAfters != null && daydealAfters.size() >= 4) {
				analysis.setRs5(daydealAfters.get(3).getRiseWidth());
				analysis.setRs5(DoubleUtil.changeDecimal(analysis.getRs5(), 1));
			}

			analysises.add(analysis);
		}
		return analysises;
	}
}
